/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-07-28 21:06:54
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-12 21:15:51
 */
import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_data_center/screens/common/user_helper.dart';
import 'package:flutter_data_center/screens/home/home_screen.dart';
import 'package:flutter_data_center/screens/login/login_screen.dart';

import 'package:flutter_data_center/utills/log_utils.dart';
import 'package:flutter_data_center/utills/navigator_utils.dart';

class WelcomeTimeWidget extends StatefulWidget {
  @override
  _WelcomeTimeWidgetState createState() => _WelcomeTimeWidgetState();
}

class _WelcomeTimeWidgetState extends State<WelcomeTimeWidget> {
  int currentTime = 5;
  Timer _timer;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (currentTime == 0) {
        _timer.cancel();
        goHome();
        return;
      }
      currentTime--;
      setState(() {});
      LogUtils.e("计时器回调 $currentTime");
    });
  }

  @override
  void dispose() {
    if (_timer.isActive) {
      _timer.cancel();
    }
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        goHome();
      },
      child: buildContainer(),
    );
  }

  void goHome() {
    LogUtils.e("计时完成 进入首页");
    // NavigatorUtils.pushPageByFade(
    //   context: context,
    //   targetPage: HomeScreen(),
    //   isReplace: true,
    // );
    if (UserHelper.getInstance.isLogin) {
      NavigatorUtils.pushPageByFade(
        context: context,
        targetPage: HomeScreen(),
        isReplace: true,
      );
    } else {
      NavigatorUtils.pushPageByFade(
        context: context,
        targetPage: LoginScreen(),
        isReplace: true,
      );
    }
  }

  Widget buildContainer() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.redAccent,
          width: 2,
        ),
        borderRadius: BorderRadius.all(
          Radius.circular(20),
        ),
      ),
      width: 100,
      height: 33,
      alignment: Alignment.center,
      child: Text(
        "${currentTime}s",
        style: TextStyle(
          fontSize: 16,
          color: Colors.redAccent,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }
}
